package com.allways.dubbo.mdm.service.dict.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import com.allways.dubbo.mdm.beans.dict.Dict;
import com.allways.dubbo.mdm.service.dict.IDictService;

/**
 * <p>ClassName: IDictService</p>
 * <p>Description: 数据字典Service接口</p>
 * <p>Author: weih</p>
 * <p>Date: 2016-10-11</p>
 */
@Service("mdmDictService")
public class DictServiceImpl extends MdmBaseDaoImpl implements IDictService {
	Log log = LogFactory.getLog(DictServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
    @Override
    public int addDict(Dict dict) {
   		log.debug("For the new dict parameters:"+ dict.toJson());
   		validColumnLen(dict);
    	
        //无code的不能插入
        if(StringUtils.isEmpty(dict.getDictCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != getDictByCode(dict.getDictCode())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        return insert("dict.insertDict",dict);
    }

    @Override
    public int updateDict(Dict dict) {
    	log.debug("Update for the dict parameters:"+ dict.toJson());
    	validColumnLen(dict);
     	//无code的不能更新
        if(StringUtils.isEmpty(dict.getDictCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        Dict oldDict = getDictByCode(dict.getDictCode());
        if(null == oldDict){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
		oldDict.setDictType(dict.getDictType());
		oldDict.setDictCode(dict.getDictCode());
		oldDict.setDictName(dict.getDictName());
		oldDict.setDictDesc(dict.getDictDesc());
		oldDict.setOrderId(dict.getOrderId());
		oldDict.setStatus(dict.getStatus());
        
        return update("dict.updateDict",oldDict);
    }

    @Override
    public int deleteDict(Dict dict) {
    	log.debug("Delete for the dict parameters:"+ dict.toJson());
     	//无code的不能删除
        if(StringUtils.isEmpty(dict.getDictCode())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        
        return delete("dict.deleteDict",dict);
    }

    @Override
    public Dict getDictByCode(String code) {
    	log.debug("Query for dict code:" + code);
        return queryForObject("dict.getDictByCode",code);
    }
    @Override
    public long getDictCount(Dict dict) {
    	log.debug("Query for dict count parameters:" + dict.toJson());
        return queryForObject("dict.getDictCount",dict);
    }
   @Override
    public List<Dict> queryDicts(Dict dict, int firstRow, int lastRow) {
      	log.debug("Query for dict parameters:" + dict.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("dict",dict);
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("dict.queryDicts", param);
    }
    
    /**
     * 验证字段长度
     * @param dict 需要验证的对象
     */
    private void validColumnLen(Dict dict){
		if(!StringUtils.isEmpty(dict.getDictType()) && dict.getDictType().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(dict.getDictCode()) && dict.getDictCode().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(dict.getDictName()) && dict.getDictName().length()>800){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{800},null);
        }
		if(!StringUtils.isEmpty(dict.getDictDesc()) && dict.getDictDesc().length()>200){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{200},null);
        }
		if(!StringUtils.isEmpty(dict.getOrderId()) && dict.getOrderId().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
		if(!StringUtils.isEmpty(dict.getStatus()) && dict.getStatus().length()>0){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{0},null);
        }
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(dict.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
